package io.github.topcatv.mall.common.security;

import io.github.topcatv.mall.common.ao.ReturnObject;
import io.github.topcatv.mall.common.util.ResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liuyi
 */
@Component("unauthorizedHandler")
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {

    /**
     * 未登录或无权限时触发的操作
     * 返回  {"code":401,"message":"你没有携带 token 或者 token 无效！","data":""}
     *
     * @param httpServletRequest request
     * @param httpServletResponse response
     * @param e exception
     * @throws IOException ioException
     */
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        ResponseUtil.responseText(httpServletResponse, ReturnObject.unauthorized());
    }

}